/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.geoEngine.scene.mesh;

import com.aionengine.gameserver.geoEngine.collision.Collidable;
import com.aionengine.gameserver.geoEngine.collision.CollisionResults;
import com.aionengine.gameserver.geoEngine.math.Ray;
import com.aionengine.gameserver.geoEngine.scene.Geometry;

import java.util.BitSet;

/**
 * @author MrPoke, Rolandas
 */
public class DoorGeometry extends Geometry {

    BitSet instances = new BitSet();
    private boolean foundTemplate = false;

    public DoorGeometry(String name) {
        super(name);
    }

    public void setDoorState(int instanceId, boolean isOpened) {
        instances.set(instanceId, isOpened);
    }

    @Override
    public int collideWith(Collidable other, CollisionResults results) {
        if (foundTemplate && instances.get(results.getInstanceId()))
            return 0;
        if (other instanceof Ray) {
            // no collision if inside arena spheres, so just check volume
            return getWorldBound().collideWith(other, results);
        }
        return super.collideWith(other, results);
    }

    public boolean isFoundTemplate() {
        return foundTemplate;
    }

    public void setFoundTemplate(boolean foundTemplate) {
        this.foundTemplate = foundTemplate;
    }

    @Override
    public void updateModelBound() {
        // duplicate call distorts world bounds, thus do only once
        if (worldBound == null) {
            mesh.updateBound();
            worldBound = getModelBound().transform(cachedWorldMat, worldBound);
        }
    }
}
